<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Ruby Names: Ruby Study Notes - Best Ruby Guide, Ruby Tutorial</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="Ruby Study Notes - Best Ruby Guide, Ruby Tutorial" />
<meta name="keywords" content="ruby names,ruby study notes,ruby guide,free ruby programming course,best ruby guide,ruby tutorials,ruby tutorial,learn ruby,ruby,ruby on rails,ruby rails,ruby learning,ruby tutoring,learning ruby,ruby programming,ruby on rails development,ruby training" />
<meta name="Distribution" content="Global" />
<meta name="author" content="Satish Talim / Original design: Erwin Aligam - ealigam@gmail.com" />
<meta name="copyright" content="Satish Talim 2007 and beyond..." />
<meta name="verify-v1" content="rFu86se+IkbtF+bH8mgJBKwU5HnKaSd8Ghw9umXQOkM=" />
<meta name="robots" content="index,follow" />
<meta http-equiv="Expires" content="0" />
<meta name="revisit-after" content="1 days" />
<link rel="stylesheet" href="/images/NewOrange.css" type="text/css" />
<link rel="stylesheet" href="/images/syntaxhighlighter.css" type="text/css" />
<link rel="icon" type="image/ico" href="/images/favicon.ico" />
<!-- Google +1 button code -->
<link rel="canonical" href="/satishtalim/ruby_names.html" />
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>

<!-- Google Analytics code -->
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-59044-10']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>
<!-- Google Analytics code ends -->

</head>

<body>
<!-- wrap starts here -->
<div id="wrap">

    <div id="header">

        <h1 id="logo">Ruby<span class="orange">Learning.github.io</span></h1>
        <h2 id="slogan">Helping Ruby Programmers become Awesome!</h2>

    </div>

    <div id="menu">
        <ul>
            <li><a href="/" title="Home page for rubylearning.github.io">Home</a></li>
            <li><a href="/satishtalim/tutorial.html" title="Get started Learning Ruby here...">Tutorial</a></li>
            <li><a href="/download/downloads.html" title="Download this tutorial as an eBook">Downloads</a></li>
            <li><a href="/other/testimonials.html" title="People around the world who benefited from this site">Testimonials</a></li>
            <li><a href="/other/certification.html" title="Get certified in Ruby">Certification</a></li>
            <li><a href="/satishtalim/ruby_guide.html" title="Ruby Guide, Mentor">Mentor</a></li>
            <li><a href="https://blog.rubylearning.github.io/" title="Ruby blog of Learning Ruby site">Blog</a></li>
            <li><a href="/satishtalim/tutorial.html" title="Online Ruby Course">Online Course</a></li>
            <li><a href="http://ruby-challenge.rubylearning.github.io/" title="Ruby Programming Challenge for Newbies">Challenge</a></li>
            <li><a href="/satishtalim/about.html" title="Information about Satish Talim">About</a></li>
        </ul>
    </div>

    <!-- content-wrap starts here -->
    <div id="content-wrap">

            <div id="main">

                <div id="main-inner"><a name="TemplateInfo"></a>
                <h1>Ruby Names</h1>

                <p class="post-footer align-right">
                  <strong>
                    <a href="/satishtalim/getting_input.html">&lt;Getting Input | </a>
                    <a href="/satishtalim/tutorial.html">TOC | </a>
                    <a href="/satishtalim/more_on_ruby_methods.html">More on Methods &gt;</a>
                  </strong>
                </p>

                <p><strong>Ruby Names</strong> are used to refer to constants, variables, methods, classes, and modules. <i>The first character of a name helps Ruby to distinguish its intended use</i>. Certain names, are reserved words and should not be used as variable, method, class, or module name. Lowercase letter means the characters "a" through "z". Uppercase letter means ''A'' through ''Z,'' and digit means "0" through "9". A name is an uppercase letter, lowercase letter, or an underscore ("_"), followed by <strong><i>Name characters</i></strong> (this is any combination of upper- and lowercase letters, underscore and digits).</p>

                <h3>Variables</h3>

                <p>Variables in Ruby can contain data of any type. You can use variables in your Ruby programs <i>without any declarations</i>. Variable name itself denotes its scope (local, global, instance, etc.).</p>

                <ul><li>A <strong>local</strong> variable (declared within an object) name consists of a lowercase letter (or an underscore) followed by name characters (sunil, _z, hit_and_run).</li><li>An <strong>instance</strong> variable (declared within an object always "belongs to" whatever object <strong>self</strong> refers to) name starts with an ''at'' sign (''@'') followed by a <em>name</em> (@sign, @_, @Counter).</li><li>A <strong>class</strong> variable (declared within a class) name starts with two ''at'' signs (''@@'') followed by a <em>name</em> (@@sign, @@_, @@Counter). A class variable is shared among all objects of a class. Only one copy of a particular class variable exists for a given class. Class variables used at the top level are defined in <strong>Object</strong> and behave like global variables. <em>Class variables are rarely used in Ruby programs</em>.</li><li><strong>Global</strong> variables start with a dollar sign (''$'') followed by name characters. A global variable name can be formed using ''$-'' followed by any single character ($counter, $COUNTER, $-x). Ruby defines a number of global variables that include other punctuation characters, such as $_ and $-K.</li></ul>

                <h3>Constants</h3>

                <p>A <strong>constant</strong> name starts with an uppercase letter followed by name characters. Class names and module names are constants, and follow the constant naming conventions. Examples: module MyMath, PI=3.1416, class MyPune.</p>

                <h3>Method Names</h3>

                <p><strong>Method</strong> names should begin with a lowercase letter (or an underscore). "?", "!" and "=" are the only weird characters allowed as method name suffixes (! or bang labels a method as dangerous-specifically, as the dangerous equivalent of a method with the same name but without the bang. More on <a href="/satishtalim/writing_own_ruby_methods.html">Bang methods</a> later.).</p>

                <p class="post-note">The <i>Ruby convention</i> is to use underscores to separate words in a multiword method or variable name. By convention, most constants are written in all uppercase with underscores to separate words, LIKE_THIS. Ruby class and module names are also constants, but they are conventionally written using initial capital letters and camel case, LikeThis.<br />It's to be noted that any given variable can at different times hold references to objects of many different types. A Ruby constant is also a reference to an object. Constants are created when they are first assigned to (normally in a class or module definition; they should not be defined in a method - more on <a href="/satishtalim/ruby_constants.html">constants</a> later). Ruby lets you alter the value of a constant, although this will generate a warning message. Also, variables in Ruby act as "references" to objects, which undergo automatic garbage collection.</p>

                <p>An example to show Ruby is <i>dynamically typed</i> -  <strong>p007dt.rb</strong></p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                # p007dt.rb
                # Ruby is dynamic
                x = 7           # integer
                x = "house"  # string
                x = 7.5        # real

                # In Ruby, everything you manipulate is an object
                'I love Ruby'.length
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>The basic types in Ruby are <strong>Numeric</strong> (subtypes include <strong>Fixnum</strong>, <strong>Integer</strong>, and <strong>Float</strong>), <strong>String</strong>, <strong>Array</strong>, <strong>Hash</strong>, <strong>Object</strong>, <strong>Symbol</strong>, <strong>Range</strong>, and <strong>RegExp</strong>.</p>

                <p class="post-note">Though we have not learned classes yet, nevertheless, here are some more details about a class called <strong>Float</strong>.<br /><br /><strong>Float</strong> is a sub class of <strong>Numeric</strong>.<br /><br /><strong>Float</strong> objects represent real numbers using the native architecture's double-precision floating point representation.<br /><br /><strong>DIG</strong> is a class constant that gives precision of <strong>Float</strong> in decimal digits. <strong>MAX</strong> is another class constant that gives the largest <strong>Float</strong>.</p>

                <p>On my PC, the code:</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                puts Float::DIG
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>outputs 15. And.</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                puts Float::MAX
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>outputs 1.79769313486232e+308</p>

                <p>Let us look at Peter Cooper's example in his <em>Beginning Ruby</em> book (never mind if you do not follow the code yet):</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                rice_on_square = 1
                64.times do |square|
                  puts "On square #{square + 1} are #{rice_on_square} grain(s)"
                  rice_on_square *= 2
                end
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>By square 64, you're up to placing many trillions of grains of rice on each square!<br /><br />It proves that Ruby is able to deal with extremely large numbers, and unlike many other programming languages, there are no inconvenient limits. Ruby does this with different classes, one called <strong>Fixnum</strong> (default) that represents easily managed smaller numbers, and another, aptly called <strong>Bignum</strong>, that represents "big" numbers Ruby needs to manage internally. Ruby will handle Bignums and Fixnums for you, and you can perform arithmetic and other operations without any problems. Results might vary depending on your system's architecture, but as these changes are handled entirely by Ruby, there's no need to worry.
                </p>

                <p>Ruby doesn't require you to use <a href="http://en.wikipedia.org/wiki/Primitive_type">primitives</a> (<a href="http://en.wikipedia.org/wiki/Data_type">data types</a>) when manipulating data of these types - if it looks like an integer, it's probably an integer; if it looks like a string, it's probably a string. The class <strong>Object</strong> has a method called <strong>class</strong> that returns the class of an object, for example:</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                s = 'hello'
                s.class  # String
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>Another example: (Don't worry if you do not understand the code now).</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                puts 'I am in class = ' + self.class.to_s
                puts 'I am an object = ' + self.to_s
                print 'The object methods are = '
                puts self.private_methods.sort
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>We shall talk about <a href="/satishtalim/ruby_self.html">self</a> later on. <strong>private_methods</strong> is a method of the <strong>Object</strong> class and <strong>sort</strong> is a method of the <strong>Array</strong> class.</p>

                <p>In Ruby, everything you manipulate is an object, and the results of those manipulations are themselves objects. There are no primitives or data-types.</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                5.times { puts "Mice!\n" } # more on blocks later
                "Elephants Like Peanuts".length
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p style="background-color: #FAFAFA; padding: 5px; margin-top: 20px; font-size: 65%;"><strong>Note</strong>: The Ruby Logo is Copyright (c) 2006, Yukihiro Matsumoto. I have made extensive references to information, related to Ruby, available in the public domain (wikis and the blogs, articles of various <span style="font-weight: bold;" title="Click Gurus on the menu above">Ruby Gurus</span>), my acknowledgment and thanks to all of them. Much of the material on <a href="/">rubylearning.github.io</a> and in the course at <a href="http://rubylearning.org/">rubylearning.org</a> is drawn <strong>primarily</strong> from the <strong>Programming Ruby book</strong>, available from <a href="http://pragprog.com/book/ruby3/programming-ruby-1-9-2-0">The Pragmatic Bookshelf</a>.</p>

                <p class="post-footer align-right">
                  <strong>
                    <a href="/satishtalim/getting_input.html">&lt;Getting Input | </a>
                    <a href="/satishtalim/tutorial.html">TOC | </a>
                    <a href="/satishtalim/more_on_ruby_methods.html">More on Methods &gt;</a>
                  </strong>
                </p>

            </div>
            <!-- main inner ends here -->
        </div>

            <div id="rightbar">

            </div>

    <!-- content-wrap ends here -->
    </div>

<!-- wrap ends here -->
</div>

<!-- footer starts here -->
<div id="footer">
    <!-- CHANGE THE FOOTER -->
    <p>&copy; 2006-2021 <strong>rubylearning.github.io - A Ruby Tutorial</strong>&nbsp;&nbsp;Page Updated: 5th Jan. 2021 | Design: <a href="mailto:ealigam@gmail.com">Erwin Aligam</a> | Valid: <a href="http://validator.w3.org/check/referer">XHTML</a> | <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/">Home</a> | <a href="/privacy.html">Privacy</a> | <a href="/sitemap.html">Sitemap</a></p>
<!-- footer ends here -->

<!-- SyntaxHighlighter code -->
<script src="/js/shCore.js" type="text/javascript"></script>
<script src="/js/shBrushRuby.js" type="text/javascript"></script>
<script type="text/javascript">
dp.SyntaxHighlighter.HighlightAll('code');
</script>
<!-- SyntaxHighlighter code -->

</body>
</html>
